//
//  DiscoverViewSearchView.m
//  weibo
//
//  Created by qiuming on 15/12/21.
//  Copyright © 2015年 qiuming. All rights reserved.
//

#import "DiscoverViewSearchView.h"
#import <Masonry.h>
#import "Extensions.h"
@interface DiscoverViewSearchView()<UITextFieldDelegate>
@property (strong, nonatomic) UIButton *searchBtn;
@property (strong, nonatomic) UITextField *inputField;
@property (strong, nonatomic) UIButton *cancelBtn;
@end

@implementation DiscoverViewSearchView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    // 添加控件
    [self addSubview:self.inputField];
    [self addSubview:self.cancelBtn];
    // 自动布局
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.mas_bottom);
        make.width.mas_equalTo(45);
    }];
    
    [self.inputField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
}

- (void)cancelBtnClick {
    [self.cancelBtn setHidden:YES];
    [self.inputField resignFirstResponder];
    self.inputField.placeholder = @"大家都在搜：周杰伦昆明演唱会";
    [self updateTextFieldConstraints:0];
    
}

// MARK: - UITextFieldDelegate
// 动画更新文本框右侧约束
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [self.cancelBtn setHidden:NO];
    self.inputField.placeholder = @"周杰伦昆明演唱会";
    [self updateTextFieldConstraints:self.cancelBtn.bounds.size.width];
}

- (void)updateTextFieldConstraints:(CGFloat)constraint {
    
    [self.inputField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, constraint));
    }];
    // 添加动画
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];

}

// MARK: - 懒加载
- (UITextField *)inputField {
    if (_inputField == nil) {
        _inputField = [[UITextField alloc]init];
        _inputField.backgroundColor = [UIColor colorWithRed:.4 green:.8 blue:1.0 alpha:.5];
        _inputField.borderColor = [UIColor darkGrayColor];
        _inputField.borderWidth = .4;
        _inputField.cornerRadius = 4;
        _inputField.layer.masksToBounds = YES;
        
        // 设置左侧视图
        UIImageView *searchImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"searchbar_textfield_search_icon"]];
        searchImage.frame = CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.height);
        searchImage.contentMode = UIViewContentModeCenter;
        _inputField.leftView = searchImage;
        _inputField.leftViewMode = UITextFieldViewModeAlways;
        
        _inputField.placeholder = @"大家都在搜：周杰伦昆明演唱会";
        _inputField.textAlignment = NSTextAlignmentLeft;
        [_inputField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [_inputField setKeyboardType:UIKeyboardTypeDefault];
        _inputField.delegate = self;
        
    }
    return _inputField;
}

- (UIButton *)cancelBtn {
    if (_cancelBtn == nil) {
        _cancelBtn = [[UIButton alloc]init];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_cancelBtn setHidden:YES];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}
@end
